package com.ruoyi.college.service.impl;

import com.ruoyi.college.domain.Class;
import com.ruoyi.college.domain.College;
import com.ruoyi.college.domain.Major;
import com.ruoyi.college.mapper.ArchiveMapper;
import com.ruoyi.college.mapper.ClassMapper;
import com.ruoyi.college.service.IArchiveService;
import com.ruoyi.college.service.ICollegeService;
import com.ruoyi.college.service.IMajorService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 班级信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-25
 */
@Service
public class ArchiveServiceImpl implements IArchiveService
{
    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private ICollegeService collegeService;

    @Autowired
    private IMajorService majorService;



    @Autowired
    private ArchiveMapper archiveMapper;




    /**
     * 查询班级信息列表
     * 
     * @param
     * @return 班级信息
     */
    @Override
    public List<Class> selectClassList(Class cla)
    {
        List<Class> classes = classMapper.selectClassList(cla);
        classes.forEach(c->{
            Integer collegeId = c.getCollegeId();
            College college = collegeService.selectCollegeByCollegeId(Long.parseLong(String.valueOf(collegeId)));
            c.setCollegeName(college.getCollegeName());
            Major major = majorService.selectMajorByMajorId(Long.parseLong(String.valueOf(c.getMajorId())));
            c.setMajorName(major.getMajorName());
        });
        return classes;
    }



    public List<Map<String, Object>> getStudentScorePivot(Class cla) {
        // 1. 查询所有实验项目（ID + 名称）
        List<Map<String, Object>> projects = archiveMapper.selectProjectsByClassId(cla.getClassId());
        if (projects.isEmpty()) {
            // 无实验项目时，返回空结果或提示
            return new ArrayList<>();
        }

        // 2. 拼接动态 CASE WHEN 语句（生成每个项目的成绩列）
        StringBuilder caseSql = new StringBuilder();
        for (Map<String, Object> project : projects) {

// 正确写法（用 BigInteger 接收）
            BigInteger id = (BigInteger) project.get("project_id");
// 如需转为 Long（确保值在 Long 范围内）
            Long projectId = id.longValue();  // 安全转换
//            Long projectId = (Long) project.get("project_id");
            String projectName = (String) project.get("project_name");
            // 处理项目名称中的特殊字符（如空格、括号），用反引号包裹列名
            String columnName = "`" + projectName + "成绩`";
            // 拼接 CASE 语句：MAX(CASE WHEN 项目ID匹配 THEN 成绩 END) AS 列名
            caseSql.append(String.format(
                    "MAX(CASE WHEN r.class_project_id = %d THEN r.score END) AS %s, ",
                    projectId, columnName
            ));
        }
        // 移除最后一个逗号和空格
        if (caseSql.length() > 0) {
            caseSql.setLength(caseSql.length() - 2);
        }

        // 3. 调用 Mapper 执行动态查询
        return archiveMapper.selectStudentScorePivot(caseSql.toString(),cla.getClassId());
    }


    public List<Map<String, Object>> selectProjectsByClassId(Long classId)
    {
        return archiveMapper.selectProjectsByClassId(classId);
    }
    @Override
    public List<Map<String, Object>> selectStudentScoreProject(Long projectId,Long classId) {
        // 1. 查询所有实验项目（ID + 名称）
        return archiveMapper.selectStudentScoreProject(projectId,classId);


     }
    @Override
    public List<Map<String, Object>> selectStudentScoreProject(Class cla) {
        // 1. 查询所有实验项目（ID + 名称）
        List<Map<String, Object>> projects = archiveMapper.selectProjectsByClassId(cla.getClassId());
        if (projects.isEmpty()) {
            // 无实验项目时，返回空结果或提示
            return new ArrayList<>();
        }

        List<Map<String, Object>> result = new ArrayList<>();

        for (Map<String, Object> project : projects) {

            BigInteger id = (BigInteger) project.get("project_id");
            Long projectId = id.longValue();  // 安全转换
            String projectName = (String) project.get("project_name");
            result = archiveMapper.selectStudentScoreProject(projectId,cla.getClassId());

        }

        return null;
    }

}
